﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections;
using System.Linq;
using TESTCommons.Encrypt;
using TESTCommons.HttpHelper;
using TESTCommons.JsonHelper;
using TESTCommons.VerificationCode;
using TestModel.SysManage;
using TestService.SysManage;

namespace TestManage.Controllers
{
    public class LoginController : Controller
    {
        #region 参数
        private readonly VerifyHelper _verifyHelper = new VerifyHelper();
        private readonly ISys_UserService _sys_UserService;
        #endregion

        #region 构造函数
        public LoginController(ISys_UserService sys_UserService)
        {
            _sys_UserService = sys_UserService;
        }
        #endregion

        /// <summary>
        /// 登陆界面
        /// </summary>
        [HttpGet]
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 注册界面
        /// </summary>
        [HttpGet]
        public IActionResult Registered()
        {
            return View();
        }

        /// <summary>
        /// 生成验证码
        /// </summary>
        [HttpGet]
        public ActionResult VerifyCode()
        {
            WebHelper.SetCookie("loginCode", _verifyHelper.Text, 20);
            return File(_verifyHelper.GetBytes(), @"image/Gif");
        }

        /// <summary>
        /// 登陆
        /// </summary>
        [HttpPost]
        public IActionResult Checked(sys_User model)
        {
            var result = ExistsUser(model);
            if (result != null)
            {
                return Json(result);
            }

            var user = _sys_UserService.Login(model);
            if (user == null)
            {
                return Json(new AjaxResult { type = ResultType.error, errorcode = 1, message = "用户不存在" });
            }
            string md5pwd = MD5Provider.Hash(model.userPwd);
            if (user.userPwd != md5pwd)
            {
                return Json(new AjaxResult { type = ResultType.error, errorcode = 4, message = "密码错误" });
            }

            AccountModel account = new AccountModel();
            account.UserID = user.gid;
            account.UserName = user.userName;
            account.IsSuperManage = user.isManage;
            WebHelper.SetSession("session_account", account);
            return Json(new AjaxResult { type = ResultType.success, message = "登陆成功" });
        }

        /// <summary>
        /// 注册
        /// </summary>
        [HttpPost]
        public IActionResult SignIn(sys_User model)
        {
            var result = ExistsUser(model);
            if (result != null)
            {
                return Json(result);
            }

            if (string.IsNullOrEmpty(model.userEmail))
            {
                return Json(new AjaxResult { type = ResultType.error, message = "Email不能为空" });
            }
            if (string.IsNullOrEmpty(model.userName))
            {
                return Json(new AjaxResult { type = ResultType.error, message = "用户名不能为空" });
            }
            var hashtable = new Hashtable();
            hashtable.Add("userLoginName", model.userLoginName);
            var user = _sys_UserService.QueryUser(hashtable).FirstOrDefault();
            if (user != null)
            {
                if (user.userEmail == model.userEmail)
                {
                    return Json(new AjaxResult { type = ResultType.error, errorcode=2, message = "邮编已经存在" });
                }
                if (user.userName == model.userName)
                {
                    return Json(new AjaxResult { type = ResultType.error, errorcode = 3, message = "用户名已经存在" });
                }
                return Json(new AjaxResult { type = ResultType.error, errorcode = 1, message = "账号已经存在" });
            }

            model.userPwd = MD5Provider.Hash(model.userPwd);
            model.gid = Guid.NewGuid();
            model.createTime = DateTime.Now;
            model.isDelete = false;
            model.isManage = false;
            model.finallyTime = DateTime.Now;
            model.userIsActivate = false;

            _sys_UserService.SignIn(model);
            return Json(new AjaxResult { type = ResultType.success, message = "注册成功" });
        }

        /// <summary>
        /// 验证输入的用户数据
        /// </summary>
        [NonAction]
        private AjaxResult ExistsUser(sys_User model)
        {
            string Verification = WebHelper.GetCookie("loginCode");
            if (string.IsNullOrEmpty(model.verifyCode))
            {
                return new AjaxResult { type = ResultType.error, errorcode = 5, message = "验证码不能为空" };
            }
            if (model.verifyCode.ToLower() != Verification.ToLower())
            {
                return new AjaxResult { type = ResultType.info, errorcode = 5, message = "验证码错误" };
            }
            if (string.IsNullOrEmpty(model.userLoginName))
            {
                return new AjaxResult { type = ResultType.error, errorcode = 1, message = "账号不能为空" };
            }
            if (string.IsNullOrEmpty(model.userPwd))
            {
                return new AjaxResult { type = ResultType.error, errorcode = 4, message = "密码不能为空" };
            }
            return null;
        }
    }
}
